package com.cmw.auntapp.core.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class SharedPreferencesUtil {
	
	Context context;
	private SharedPreferences preferences;
	/* SharedPreferences 名字 */
	public static final String SHAREDPREFERENCES_NAME = "smartbed";
	/* SharedPreferences mode*/
	public static final int MODE_PRIVATE = 0;

	public static int MODE_PUBLIC = Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE;

	public static SharedPreferencesUtil instance;

	private static boolean isPublic = false;

	/**
	 * <默认构造函数>
	 * 
	 * @param context
	 */
	private SharedPreferencesUtil(Context context) {
		this.context = context;
		preferences = context.getSharedPreferences(SHAREDPREFERENCES_NAME,
				Context.MODE_PRIVATE);
	}

	/**
	 * <默认构造函数>
	 *
	 * @param context
	 */
	private SharedPreferencesUtil(Context context, int mode) {
		this.context = context;
		preferences = context.getSharedPreferences(SHAREDPREFERENCES_NAME,
				mode);
	}

	/**
	 * 静态实例方法，返回一个有公共读定的 SharedPreferences
	 * 不同APP间共享数据可用此实例
	 * @param context
	 */
	public static SharedPreferencesUtil getPubInstance(Context context) {
		if (instance == null) {
			instance = new SharedPreferencesUtil(context, MODE_PUBLIC);
		}else{
			if(!isPublic){
				instance = new SharedPreferencesUtil(context, MODE_PUBLIC);
			}
		}
		isPublic = true;
		return instance;
	}
	/**
	 * 静态实例方法
	 * 
	 * @param context
	 */
	public static SharedPreferencesUtil getInstance(Context context) {
		if (instance == null) {
			instance = new SharedPreferencesUtil(context);
		}else{
			if(isPublic){
				instance = new SharedPreferencesUtil(context);
			}
		}
		isPublic = false;
		return instance;
	}

	

	/**
	 * <获得编辑器> <找开编辑器,写入数据后调用commit(),会自动关闭>
	 * 
	 * @return android.content.SharedPreferences.Editor [编辑器]
	 */
	public Editor getEditor() {
		return preferences.edit();
	}


	
	/**
	 * 保存string值
	 * 
	 * @param key
	 * @param value
	 */
	public void saveString(String key, String value) {
		preferences.edit().putString(key, value).commit();
	}

	/**
	 * 保存Map值
	 *
	 * @param key key值
	 * @param obj	要保存的Map对象
	 */
	public void saveMap(String key, Map<String, Object> obj) {
		saveObject(key, obj);
	}

	/**
	 * 获取指定类型的对象值
	 * @param key	key值
	 * @return	返回Map对象
	 */
	public Map<String, Object> getMap(String key) {
		String value = preferences.getString(key, "");
		Map<String, Object> object = FastJsonUtil.convertJsonToObj(value, Map.class);
		return object;
	}

	/**
	 * 保存对象T的值
	 *
	 * @param key key值
	 * @param obj	要保存的对象
	 */
	public <T> void saveObject(String key, T obj) {
		String value = FastJsonUtil.convertJsonToStr(obj);
		preferences.edit().putString(key, value).commit();
	}

	/**
	 * 验证指定的键是否存在
	 * @param key	key值
	 * @return	返回指定Key是否存在 [true:存在, false:不存在]
	 */
	public boolean existKey(String key) {
		return preferences.contains(key);
	}

	/**
	 * 获取指定类型的对象值
	 * @param key	key值
	 * @param clz	对象类型
	 * @param <T>	泛型类型
	 * @return	返回指定对象
	 */
	public <T> T getObject(String key, Class<T> clz) {
		String value = preferences.getString(key, "");
		if (!StringHandler.isValidStr(value)) {
			return null;
		}
		T object = FastJsonUtil.convertJsonToObj(value, clz);
		return object;
	}

	/**
	 * 获取指定类型的列表值
	 * @param key	key值
	 * @param clz	对象类型
	 * @param <T>	泛型类型
	 * @return	返回List对象
	 */
	public <T> List<T> getList(String key, Class<T> clz) {
		String value = preferences.getString(key, "");
		if (!StringHandler.isValidStr(value)) {
			return null;
		}
		List<T> dataList = FastJsonUtil.convertJsonToList(value, clz);
		return dataList;
	}
	
	/**
	 * 取string值
	 * @param key
	 * @param defValue
	 * @return
	 */
	public String getString(String key, String... defValue) {
		if (defValue.length > 0)
			return preferences.getString(key, defValue[0]);
		else
			return preferences.getString(key, "");
	}
	/**
	 * 取string值
	 * @param key
	 * @param defValue
	 * @return
	 */
	public int getInt(String key, int... defValue) {
		if (defValue.length > 0)
			return preferences.getInt(key, defValue[0]);
		else
			return preferences.getInt(key, 0);
		
	}

	/**
	 * 删除数据
	 * @param keys 要删除的数据key数组
	 */
	public void remove(String... keys) {
		for(String key : keys) {
			preferences.edit().remove(key).commit();
		}
	}

	/**
	 * 保存boolean值ֵ
	 * 
	 * @param key
	 * @param value
	 */
	public void saveBoolean(String key, Boolean value) {
		preferences.edit().putBoolean(key, value).commit();
	}
	/**
	 * 保存int值ֵ
	 * 
	 * @param key
	 * @param value
	 */
	public void saveInteger(String key, Integer value) {
		preferences.edit().putInt(key, value).commit();
	}

	/**
	 * 保存long值ֵ
	 *
	 * @param key
	 * @param value
	 */
	public void saveLong(String key, Long value) {
		preferences.edit().putLong(key, value).commit();
	}


	/**
	 * 取long值
	 * @param key
	 * @param defValue
	 * @return
	 */
	public long getLong(String key, long... defValue) {
		if (defValue.length > 0)
			return preferences.getLong(key, defValue[0]);
		else
			return preferences.getLong(key, 0);

	}


	/**
	 * 取boolean类型的值
	 * @param key
	 * @param defValue
	 * @return
	 */
	public Boolean getBoolean(String key, Boolean... defValue) {
		if (defValue.length > 0)
			return preferences.getBoolean(key, defValue[0]);
		else
			return preferences.getBoolean(key, false);

	}



}
